package com.abhimanyu.bootstrap.tasklets;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.abhimanyu.bootstrap.enums.BootstrapConstants;
import com.abhimanyu.bootstrap.util.JobContext;
import com.abhimanyu.service.ICurrencyRateObserver;

@Component
public class DollarDataRetrieverTasklet implements Tasklet {
	
	@Autowired
	private List<ICurrencyRateObserver> currencyRateObserverList;

	public RepeatStatus execute(StepContribution stepContribution,
			ChunkContext context) throws Exception {
		List<String> dollarRates = getDollarRatesFromServices();
		Logger.getLogger(DollarDataRetrieverTasklet.class).debug("TEST");
		System.out.println("*************************** DOLLAR RATES*************************** ");
		System.out.println(dollarRates);
		JobContext.getInstance().put(BootstrapConstants.DOLLAR_RATES, dollarRates);
		return RepeatStatus.FINISHED;
	}

	private List<String> getDollarRatesFromServices() {
		System.out.println("STARTING RETRIEVAL");
		List<String> dollarRates = new ArrayList<String>();
		for (ICurrencyRateObserver service : currencyRateObserverList) {
			dollarRates.add(service + " : " + service.getCurrentDollarRate());
		}
		return dollarRates;
	}

	public void setCurrencyRateObserverList(
			List<ICurrencyRateObserver> currencyRateObserverList) {
		this.currencyRateObserverList = currencyRateObserverList;
	}
}
